package com.b511.swpu.cementbackend.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.util.*;

@Entity
@Getter
@Setter
@Accessors(chain = true)
@ToString
public class SysUser implements Authentication {

    @Id
    @GenericGenerator(name = "user_generator", strategy = "uuid")
    @GeneratedValue(generator = "user_generator")
    private String id;

    private String name;

    @Column(unique = true, nullable = false)
    @JsonIgnore
    private String username;

    @Column(nullable = false)
    @JsonIgnore
    private String password;

    private boolean isEnable;

    @JsonIgnore
    private String token;

    // TODO: 4.2 密码输错3次，在固井时间内不能登陆
    @JsonIgnore
    private Integer missNumber;

    @JsonIgnore
    private Long missTime;

    @JsonIgnore
    private Long allowTime;

    @Temporal(TemporalType.DATE)
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date birthDay;

    private boolean isMan;

    @JsonIgnore
    private Long lastLoginTime;

    @OneToOne(cascade = CascadeType.REMOVE)
    @JoinColumn(name = "unit_id")
    private MetaSystemUnit metaSystemUnit;

    @OneToOne(cascade = CascadeType.REMOVE)
    @JoinColumn(name = "company_id")
    private SYSM_Company company;

    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
    private List<SysRole> roles;

    @Transient
    private String url;

    @Transient
    private boolean isAuthenticated;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @Override
    public Object getCredentials() {
        return token;
    }

    @Override
    public Object getDetails() {
        return url;
    }

    @Override
    public Object getPrincipal() {
        return null;
    }

    @Override
    public boolean isAuthenticated() {
        return isAuthenticated;
    }

    @Override
    public void setAuthenticated(boolean b) throws IllegalArgumentException {
        isAuthenticated = b;
    }
}
